<?php
namespace Framework\Library\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Message;
use Framework\Library\Tool;

class ImageValidator extends BinaryValidator
{
    public function validate(Validation $validation, $attribute)
    {
        if (parent::validate($validation, $attribute) == false) {
            return false;
        }

        $supportedImageMimeTypes = [
            'image/png',
            'image/jpeg',
            'image/gif',
            'image/webp',
        ];

        $mimeType = Tool::getMimeType($_FILES[$attribute]['tmp_name']);
        if (in_array($mimeType, $supportedImageMimeTypes)) {
            return true;
        }

        $validation->appendMessage(new Message('Unsupported image type: ' . $mimeType, $attribute, ''));

        return false;
    }
}
